package com.rashith.metro.alfa;

import java.util.List;

import com.rashith.metro.alfa.modles.WebPlace;
import com.rashith.metro.alfa.webservices.MyWebCallInterface;
import com.rashith.metro.alfa.webservices.MyWebCallingClass;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.location.Location;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class PlaceListActivity extends Activity implements MyWebCallInterface {

	public static final String CATEGORY = "cat";
	private ListView lv;
	private MyWebCallingClass webCall;
	private Location location;
	private String cat;
	private float rad;
	private SharedPreferences pref;
	private TextView count;
	private MyWebCallInterface myIn;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.place_list_view);

		lv = (ListView) findViewById(R.id.listView1);

		location = new Location("ras");
		location.setLatitude(6.912714);
		location.setLongitude(79.850786);
		location.setAltitude(12);

		cat = getIntent().getExtras().getString(PlaceListActivity.CATEGORY);
		rad = 100;

		checkOriantation();

		setTitle(cat);

		count = (TextView) findViewById(R.id.textViewCount);
		count.setText("0 places found.");

		pref = PreferenceManager.getDefaultSharedPreferences(this);

		rad = Float.parseFloat(pref.getString("distance", "15"));

		
		
		webCall = new MyWebCallingClass(PlaceListActivity.this, 6.912714,
				79.850786, cat, rad, this);
		webCall.execute("");
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		rad = Float.parseFloat(pref.getString("distance", "15"));

		webCall = new MyWebCallingClass(PlaceListActivity.this, 6.912714,
				79.850786, cat, rad, this);
		webCall.execute("");
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.cat_menu, menu);
		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		// TODO Auto-generated method stub

		switch (item.getItemId()) {
		case R.id.Settings:
			startActivity(new Intent(PlaceListActivity.this, AppPref.class));
		}
		return true;
	}

	private boolean checkOriantation() {
		if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {

			Intent intent = new Intent(PlaceListActivity.this,
					RashARActivity.class);
			intent.putExtra(ExploreActivity.CATAGORY, cat);
			startActivity(intent);
			return true;
		}
		return false;
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		//super.onBackPressed();
		Intent intent = new Intent(PlaceListActivity.this,
				ExploreActivity.class);
		startActivity(intent);
	}

	@Override
	public void doWork(List<WebPlace> p) {

		if (p != null) {
			if (lv != null) {
				PlaceListAdapter adapter = new PlaceListAdapter(
						PlaceListActivity.this, R.layout.place_list_row, p);
				lv.setAdapter(adapter);
			}
			count.setText(p.size() + " places found");
		} else {
			Toast.makeText(getApplicationContext(), "Webservice is not found",
					Toast.LENGTH_LONG).show();
			setContentView(R.layout.retry);
			Button reTry = (Button) findViewById(R.id.button1);
			reTry.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {

					setContentView(R.layout.place_list_view);

					lv = (ListView) findViewById(R.id.listView1);
					//webCall = new MyWebCallingClass(getApplicationContext(), 6.912714,79.850786, cat, rad, PlaceListActivity.this);
					//webCall.execute("");
				}
			});

		}
	}

	class PlaceListAdapter extends ArrayAdapter<WebPlace> {

		private List<WebPlace> list;
		private int rowId;

		public PlaceListAdapter(Context context, int Id, List<WebPlace> objects) {
			super(context, Id, objects);

			rowId = Id;
			list = objects;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			final WebPlace place = list.get(position);
			LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			View row = inflater.inflate(rowId, parent, false);

			TextView name = (TextView) row.findViewById(R.id.textViewName);
			name.setText(place.getName());

			TextView catagory = (TextView) row.findViewById(R.id.textView1);
			catagory.setText(place.getCatogory());

			TextView adress = (TextView) row.findViewById(R.id.textViewAddress);
			adress.setText(place.getAddress());
			
			String icon_name = "i" + place.getID();
			int x = getResources().getIdentifier(icon_name, "drawable",
					getPackageName());
			
			if( x != 0){
				ImageView iv = (ImageView) row.findViewById(R.id.imageViewLogo);
				iv.setImageResource(x);
			}

			RelativeLayout r = (RelativeLayout) row
					.findViewById(R.id.backImage);

			OnClickListener butListner = new OnClickListener() {

				@Override
				public void onClick(View v) {
					switch (v.getId()) {

					case R.id.imageButtonCall:
						String no = "tel:" + place.getPhone().trim();
						startActivityForResult(new Intent(Intent.ACTION_DIAL,
								Uri.parse(no)), 1);
						break;

					case R.id.imageButtonMap:
						String map_string = "geo:" + place.getLatitude() + ","
								+ place.getLongitude() + "?z=16";
						startActivity(new Intent(
								android.content.Intent.ACTION_VIEW,
								Uri.parse(map_string)));
						break;

					}

				}
			};

			// /////image buttons /////////////////////////////////
			ImageButton call_button = (ImageButton) row
					.findViewById(R.id.imageButtonCall);
			call_button.setOnClickListener(butListner);

			ImageButton map_but = (ImageButton) row
					.findViewById(R.id.imageButtonMap);
			map_but.setOnClickListener(butListner);
			// ////////////////////////////////////////////////

			try {
				int resId = getResources().getIdentifier("l_" + place.getID(),
						"drawable", getPackageName());
				r.setBackgroundResource(resId);

			} catch (Exception e) {
				r.setBackgroundColor(getResources().getColor(
						android.R.color.black));
			}

			return row;
		}
	}

}
